package com.hd.tiyu.yuejian.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.hd.tiyu.yuejian.Activity.AddShaituActivity;
import com.hd.tiyu.yuejian.Adapter.ShaituAdapter;
import com.hd.tiyu.yuejian.JavaBean.ShaituDetail;
import com.hd.tiyu.yuejian.Model.ListsModel;
import com.hd.tiyu.yuejian.R;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/9/14.
 */

public class ShaituFragment extends Fragment {
    private View view;
    private RecyclerView recyclerView;
    private ImageView add;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.shaitutab,container,false);
        initView();
        initData();
        initEvent();
        return view;
    }

    private void initView() {
        add=view.findViewById(R.id.shaitu_add);
        recyclerView=view.findViewById(R.id.re_shaitu);
    }

    private void initData() {
        ListsModel listsmodel=new ListsModel();
        Call<List<ShaituDetail>> call=listsmodel.getlistsShaireply();
        call.enqueue(new Callback<List<ShaituDetail>>() {
            @Override
            public void onResponse(Call<List<ShaituDetail>> call, Response<List<ShaituDetail>> response) {
                List<ShaituDetail> data=response.body();
                recyclerView.setAdapter(new ShaituAdapter(getActivity(),data));
                LinearLayoutManager manager=new GridLayoutManager(getActivity(),2);
                recyclerView.setLayoutManager(manager);
            }

            @Override
            public void onFailure(Call<List<ShaituDetail>> call, Throwable t) {

            }
        });
    }

    private void initEvent() {
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getActivity(), AddShaituActivity.class);
                startActivity(intent);
            }
        });
    }
}
